package com.tanhua.server.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${tanhua.sso.url}")
    private String ssoUrl;
    private static final ObjectMapper objectMapper=new ObjectMapper();
    @Override
    public User getUserByToken(String token) {
        String url=ssoUrl+"/user/"+token;
        try {
            String json = restTemplate.getForObject(url, String.class);
            if(null==json){
                return null;
            }
            User user = objectMapper.readValue(json, User.class);
            return user;
        } catch (Exception e) {
           log.error("token解析错误,token=" +token);
        }
        return null;
    }
}
